package com.lanou.dllo.shise.com.lanou.dllo.fragment;



import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Space;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.dllo.shise.R;
import com.lanou.dllo.shise.com.lanou.dllo.adapter.SameCityListAdapter;
import com.lanou.dllo.shise.com.lanou.dllo.entity.DataBean;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * A simple {@link Fragment} subclass.
 */
public class Same_cityFragment extends Fragment {
    private RequestQueue queue;
    private SameCityListAdapter adapter;
    private RecyclerView sameCityListView;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_same_city, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        queue = Volley.newRequestQueue(getActivity());
        sameCityListView = (RecyclerView) getActivity().findViewById(R.id.sameCityListView);
        adapter = new SameCityListAdapter(getActivity());
        sameCityListView.setLayoutManager(new StaggeredGridLayoutManager
                (2, StaggeredGridLayoutManager.VERTICAL));
        sameCityListView.setAdapter(adapter);



    }

    private void initData() {
        String url = "http://api.meishixing.com/picture/picturelist/last/page=1&city_id=7&session_id=00001227993a5661b25da19c79c2a7c9a6affc";
        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Log.i("lanou", "----" + response);
                DataBean dataBean = gson.fromJson(response, DataBean.class);
                if (dataBean != null) {
                    List<DataBean.ResultEntity> datas = dataBean.getResult();
                    if (datas != null && datas.size() > 0) {
                        adapter.addData(datas);
                    }

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }

            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }

        };
        queue.add(stringRequest);
    }


    }







